package com.jcfk.system.service;

import com.jcfk.system.domain.po.OaKeyworkTaskindex;
import com.jcfk.system.domain.query.OaKeyworkTaskindexQuery;
import com.jcfk.system.domain.dto.OaKeyworkTaskindexDTO;
import com.jcfk.system.dao.OaKeyworkTaskindexDao;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import com.alibaba.druid.util.StringUtils;

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

import com.jcfk.common.core.utils.IdUtils;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.Date;

/**
 * 重点工作任务指标
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-03-26
 */
@Service
public class OaKeyworkTaskindexService extends BaseService<OaKeyworkTaskindexDao, OaKeyworkTaskindex> {

    /**
     * OaKeyworkTaskindex分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<OaKeyworkTaskindexDTO> queryByPage(DataPage<OaKeyworkTaskindexDTO> page, OaKeyworkTaskindexQuery query) {
        // 分页查询
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


    /**
     * 判断OaKeyworkTaskindex是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(OaKeyworkTaskindexDTO dto) {
        QueryWrapper<OaKeyworkTaskindex> queryWrapper = new QueryWrapper<>();

        return RetResult.ok();
    }

    /**
     * 插入重点工作任务指标
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(OaKeyworkTaskindexDTO dto) {

        // 保存
        OaKeyworkTaskindex item = new OaKeyworkTaskindex();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新重点工作任务指标
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(OaKeyworkTaskindexDTO dto) {

        // 保存
        OaKeyworkTaskindex item = new OaKeyworkTaskindex();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }


    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(OaKeyworkTaskindex item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

    }

    /**
     * 根据主键获取重点工作任务指标信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public OaKeyworkTaskindexDTO get(String id) {
        // 查询数据
        OaKeyworkTaskindex item = this.baseMapper.selectById(id);
        OaKeyworkTaskindexDTO dto = new OaKeyworkTaskindexDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }


    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<OaKeyworkTaskindexDTO>
     */
    public List<OaKeyworkTaskindexDTO> getList(OaKeyworkTaskindexQuery query) {
        // 查询条件
        QueryWrapper<OaKeyworkTaskindex> qw = new QueryWrapper<>();
        // 查询数据
        List<OaKeyworkTaskindex> list = super.list(qw);

        return convertList(list, OaKeyworkTaskindexDTO.class);
    }


    /**
     *
     * @param dtos
     * @return void
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveBatch(List<OaKeyworkTaskindexDTO> dtos, String taskId, String keyworkId) {
        List<OaKeyworkTaskindex> insertList = new ArrayList<>();

        QueryWrapper<OaKeyworkTaskindex> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(OaKeyworkTaskindex::getTaskId, taskId);
        queryWrapper.lambda().eq(OaKeyworkTaskindex::getKeyworkId, keyworkId);

        // 默认值填充
        for (OaKeyworkTaskindexDTO dto : dtos) {
            OaKeyworkTaskindex item = new OaKeyworkTaskindex();
            BeanUtils.copyProperties(dto, item);
            this.setDefaultValue(item);
            item.setTaskId(taskId);
            item.setKeyworkId(keyworkId);
            insertList.add(item);
        }
        // 删除历史
        this.remove(queryWrapper);

        // 入库
        if (insertList.size() > 0)
            this.saveBatch(insertList);
    }

}

